//
//  GQHFinalExpenseView.m
//  Xanthium
//
//  Created by GuanQinghao on 14/05/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHFinalExpenseView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"
#import <UIImageView+WebCache.h>

#import "GQHSlideView.h"


@interface GQHFinalExpenseView () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    
    NSArray *rowTitleArray;
    NSArray *sectionTitleArray;
    NSArray *_placeHolderArray;
    
    UIView *receiptView;
}
@end


@implementation GQHFinalExpenseView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    sectionTitleArray = @[@"回执单",@"已付预付款",@"尾款",@"其他费用",@"运费金额"];
    rowTitleArray = @[@"油卡",@"ETC",@"现金",@"已付预付款合计",@"油卡",@"ETC",@"现金",@"尾款合计",@"油卡",@"ETC",@"现金",@"其他费用合计",@"备注",@"运费总计"];
    _qh_contentArray = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""]];
    _placeHolderArray = @[@"0.00",@"0.00",@"0.00",@"0.00",@"0.00",@"0.00",@"0.00",@"0.00",@"",@"",@"",@"",@"",@"0.00"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return sectionTitleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
            
        case 0:
            return 1;
            break;
        case 1:
            return 4;
            break;
            
        case 2:
            return 4;
            break;
            
        case 3:
            return 5;
            break;
        case 4:
            return 1;
            break;
        default:
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section && 0 == indexPath.row) {
        
        NSInteger rowNumber = _qh_imageArray.count/4 + 1;
        
        return rowNumber * (0.25 * (self.qh_width - GQH_MARGIN_DOUBLE - 30) + 10) + 10;
    }
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section && 0 == indexPath.row) {
        
        static NSString *identifier = @"aCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        receiptView = [[UIView alloc] init];
//        receiptView.backgroundColor = [UIColor redColor];
        [cell.contentView addSubview:receiptView];
        [receiptView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.mas_equalTo(cell.contentView).with.inset(16);
            make.top.and.bottom.mas_equalTo(cell.contentView);
        }];
        
        
        [self layoutViewWithImages:_qh_imageArray];
        
        return cell;
    }
    
    static NSString *identifier = @"bCell";
    YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil] firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.enterText.delegate = self;
    cell.isEnter = YES;
    cell.perLabel.text = @"元";
    cell.enterText.keyboardType = UIKeyboardTypeDecimalPad;
    
    switch (indexPath.section) {
            
        case 1: {
            cell.enterText.tag = 10 + indexPath.row;
            cell.enterText.text = _qh_contentArray[indexPath.row];
            cell.titleLabel.text = rowTitleArray[indexPath.row];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row];
            cell.enterText.enabled = NO;
        }
            break;
        case 2: {
            cell.enterText.tag = 20 + indexPath.row;
            cell.enterText.text = _qh_contentArray[indexPath.row + 4];
            cell.titleLabel.text = rowTitleArray[indexPath.row + 4];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row + 4];
            if (indexPath.row == 3)
            {
                cell.enterText.enabled = NO;
            }
        }
            break;
        case 3: {
            cell.enterText.tag = 30 + indexPath.row;
            cell.enterText.text = _qh_contentArray[indexPath.row + 8];
            cell.titleLabel.text = rowTitleArray[indexPath.row + 8];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row + 8];
            if (indexPath.row == 3)
            {
                cell.enterText.enabled = NO;
            }
            else if (indexPath.row == 4)
            {
                cell.perLabel_width.constant = 0;
                cell.enterText.keyboardType = UIKeyboardTypeDefault;
            }
        }
            break;
        case 4: {
            cell.enterText.tag = 40 + indexPath.row;
            cell.enterText.text = _qh_contentArray[indexPath.row + 13];
            cell.titleLabel.text = rowTitleArray[indexPath.row + 13];
            cell.enterText.placeholder = _placeHolderArray[indexPath.row + 13];
            cell.enterText.enabled = NO;
        }
            break;
        default:
            
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
//
//        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
//    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = sectionTitleArray[section];
    titleLabel.textColor = kAppRedColor;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(headerView);
        make.left.and.right.mas_equalTo(headerView).with.inset(GQH_MARGIN_SINGLE);
    }];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)layoutViewWithImages:(NSMutableArray<UIImage *> *)images {
    
    if (receiptView) {
        
        [receiptView qh_removeAllSubviews];
        
        
        CGFloat itemX = GQH_MARGIN_SINGLE;
        CGFloat itemY = 10;
        CGFloat itemWidth = 0.25 * (self.qh_width - GQH_MARGIN_DOUBLE - 30);
        CGFloat itemHeight = itemWidth;
        
        /// 一到五张图
        for (int i = 0; i < images.count; i++) {
            
            CGRect frame = CGRectMake(itemX  + (itemWidth + 10) * (i%4), itemY + (itemHeight + 10) * (i/4), itemWidth, itemHeight);
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
            NSString *imagePath = [NSString stringWithFormat:@"%@%@",[NSString qh_imageURLString],images[i]];
            [imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
            imageView.tag = i;
            imageView.userInteractionEnabled = YES;
            [receiptView addSubview:imageView];
            
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewImageAction:)];
            [imageView addGestureRecognizer:tap];
        }
    }
}

- (void)viewImageAction:(UITapGestureRecognizer *)tap
{
    NSInteger tag = tap.view.tag;
    NSString * urlString = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_imageArray[tag]];
    CGRect rect1 = [tap.view convertRect:tap.view.frame fromView:tap.view.superview];//获取imagevuew在subview的位置
    CGRect rect2 = [tap.view convertRect:rect1 toView:self];
    
    YHPhotoBrowser *photoView = [[YHPhotoBrowser alloc]init];
    photoView.sourceView = self;//图片所在的父容器
    photoView.urlImgArr = @[urlString]; //网络链接图片的数组
    photoView.sourceRect = rect2; // 图片的frame
    photoView.indexTag = 0;   //初始化进去显示的图片下标
    [photoView show];

}

- (IBAction)checkPhotos:(id)sender {
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSInteger tag = textField.tag / 10;
    NSInteger index = textField.tag % 10;
    
    switch (tag) {
            
            break;
        case 2:
        {
            [_qh_contentArray replaceObjectAtIndex:index + 4 withObject:textField.text];
            
            UITextField * total = (UITextField *)[self.qh_tableView viewWithTag:23];
            total.text = [NSString stringWithFormat:@"%.2f", [_qh_contentArray[0 + 4] floatValue] + [_qh_contentArray[1 + 4] floatValue] + [_qh_contentArray[2 + 4] floatValue]];
            [_qh_contentArray replaceObjectAtIndex:7 withObject:total.text];
        }
            break;
        case 3: {
            [_qh_contentArray replaceObjectAtIndex:index + 8 withObject:textField.text];
            
            UITextField * total = (UITextField *)[self.qh_tableView viewWithTag:33];
            total.text = [NSString stringWithFormat:@"%.2f", [_qh_contentArray[0 + 8] floatValue] + [_qh_contentArray[1 + 8] floatValue] + [_qh_contentArray[2 + 8] floatValue]];
            [_qh_contentArray replaceObjectAtIndex:3 + 8 withObject:total.text];
        }
            break;
        default:
            
            break;
    }
    
    UITextField * total = (UITextField *)[self.qh_tableView viewWithTag:40];
    if (total)
    {
        total.text = [NSString stringWithFormat:@"%.2f", [_qh_contentArray[3] floatValue] + [_qh_contentArray[7] floatValue] + [_qh_contentArray[11] floatValue]];
        [_qh_contentArray replaceObjectAtIndex:13 withObject:total.text];
    }
}

@end
